<!-- HTML header for doxygen 1.8.6-->
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<meta name="generator" content="Doxygen 1.8.13"/>
<title>OpenCV: samples/cpp/tutorial_code/videoio/video-write/video-write.cpp</title>
<link href="../../opencv.ico" rel="shortcut icon" type="image/x-icon" />
<link href="../../tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="../../jquery.js"></script>
<script type="text/javascript" src="../../dynsections.js"></script>
<script type="text/javascript" src="../../tutorial-utils.js"></script>
<link href="../../search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="../../search/searchdata.js"></script>
<script type="text/javascript" src="../../search/search.js"></script>
<script type="text/x-mathjax-config">
  MathJax.Hub.Config({
    extensions: ["tex2jax.js", "TeX/AMSmath.js", "TeX/AMSsymbols.js"],
    jax: ["input/TeX","output/HTML-CSS"],
});
//<![CDATA[
MathJax.Hub.Config(
{
  TeX: {
      Macros: {
          matTT: [ "\\[ \\left|\\begin{array}{ccc} #1 & #2 & #3\\\\ #4 & #5 & #6\\\\ #7 & #8 & #9 \\end{array}\\right| \\]", 9],
          fork: ["\\left\\{ \\begin{array}{l l} #1 & \\mbox{#2}\\\\ #3 & \\mbox{#4}\\\\ \\end{array} \\right.", 4],
          forkthree: ["\\left\\{ \\begin{array}{l l} #1 & \\mbox{#2}\\\\ #3 & \\mbox{#4}\\\\ #5 & \\mbox{#6}\\\\ \\end{array} \\right.", 6],
          forkfour: ["\\left\\{ \\begin{array}{l l} #1 & \\mbox{#2}\\\\ #3 & \\mbox{#4}\\\\ #5 & \\mbox{#6}\\\\ #7 & \\mbox{#8}\\\\ \\end{array} \\right.", 8],
          vecthree: ["\\begin{bmatrix} #1\\\\ #2\\\\ #3 \\end{bmatrix}", 3],
          vecthreethree: ["\\begin{bmatrix} #1 & #2 & #3\\\\ #4 & #5 & #6\\\\ #7 & #8 & #9 \\end{bmatrix}", 9],
          cameramatrix: ["#1 = \\begin{bmatrix} f_x & 0 & c_x\\\\ 0 & f_y & c_y\\\\ 0 & 0 & 1 \\end{bmatrix}", 1],
          distcoeffs: ["(k_1, k_2, p_1, p_2[, k_3[, k_4, k_5, k_6 [, s_1, s_2, s_3, s_4[, \\tau_x, \\tau_y]]]]) \\text{ of 4, 5, 8, 12 or 14 elements}"],
          distcoeffsfisheye: ["(k_1, k_2, k_3, k_4)"],
          hdotsfor: ["\\dots", 1],
          mathbbm: ["\\mathbb{#1}", 1],
          bordermatrix: ["\\matrix{#1}", 1]
      }
  }
}
);
//]]>
</script><script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.0/MathJax.js"></script>
<link href="../../doxygen.css" rel="stylesheet" type="text/css" />
<link href="../../stylesheet.css" rel="stylesheet" type="text/css"/>
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<!--#include virtual="/google-search.html"-->
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr style="height: 56px;">
  <td id="projectlogo"><img alt="Logo" src="../../opencv-logo-small.png"/></td>
  <td style="padding-left: 0.5em;">
   <div id="projectname">OpenCV
   &#160;<span id="projectnumber">4.5.2</span>
   </div>
   <div id="projectbrief">Open Source Computer Vision</div>
  </td>
 </tr>
 </tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.8.13 -->
<script type="text/javascript">
var searchBox = new SearchBox("searchBox", "../../search",false,'Search');
</script>
<script type="text/javascript" src="../../menudata.js"></script>
<script type="text/javascript" src="../../menu.js"></script>
<script type="text/javascript">
$(function() {
  initMenu('../../',true,false,'search.php','Search');
  $(document).ready(function() { init_search(); });
});
</script>
<div id="main-nav"></div>
</div><!-- top -->
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
     onmouseover="return searchBox.OnSearchSelectShow()"
     onmouseout="return searchBox.OnSearchSelectHide()"
     onkeydown="return searchBox.OnSearchSelectKey(event)">
</div>

<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<iframe src="javascript:void(0)" frameborder="0" 
        name="MSearchResults" id="MSearchResults">
</iframe>
</div>

<div class="header">
  <div class="headertitle">
<div class="title">samples/cpp/tutorial_code/videoio/video-write/video-write.cpp</div>  </div>
</div><!--header-->
<div class="contents">
<p>Check <a class="el" href="../../d7/d9e/tutorial_video_write.html">the corresponding tutorial</a> for more details</p>
<div class="fragment"><div class="line"><span class="preprocessor">#include &lt;iostream&gt;</span> <span class="comment">// for standard I/O</span></div><div class="line"><span class="preprocessor">#include &lt;string&gt;</span>   <span class="comment">// for strings</span></div><div class="line"></div><div class="line"><span class="preprocessor">#include &lt;<a class="code" href="../../d0/d9c/core_2include_2opencv2_2core_8hpp.html">opencv2/core.hpp</a>&gt;</span>     <span class="comment">// Basic OpenCV structures (cv::Mat)</span></div><div class="line"><span class="preprocessor">#include &lt;<a class="code" href="../../dc/d3d/videoio_8hpp.html">opencv2/videoio.hpp</a>&gt;</span>  <span class="comment">// Video write</span></div><div class="line"></div><div class="line"><span class="keyword">using namespace </span><a class="code" href="../../d8/dcc/namespacestd.html">std</a>;</div><div class="line"><span class="keyword">using namespace </span><a class="code" href="../../d2/d75/namespacecv.html">cv</a>;</div><div class="line"></div><div class="line"><span class="keyword">static</span> <span class="keywordtype">void</span> help()</div><div class="line">{</div><div class="line">    cout</div><div class="line">        &lt;&lt; <span class="stringliteral">&quot;------------------------------------------------------------------------------&quot;</span> &lt;&lt; endl</div><div class="line">        &lt;&lt; <span class="stringliteral">&quot;This program shows how to write video files.&quot;</span>                                   &lt;&lt; endl</div><div class="line">        &lt;&lt; <span class="stringliteral">&quot;You can extract the R or G or B color channel of the input video.&quot;</span>              &lt;&lt; endl</div><div class="line">        &lt;&lt; <span class="stringliteral">&quot;Usage:&quot;</span>                                                                         &lt;&lt; endl</div><div class="line">        &lt;&lt; <span class="stringliteral">&quot;./video-write &lt;input_video_name&gt; [ R | G | B] [Y | N]&quot;</span>                          &lt;&lt; endl</div><div class="line">        &lt;&lt; <span class="stringliteral">&quot;------------------------------------------------------------------------------&quot;</span> &lt;&lt; endl</div><div class="line">        &lt;&lt; endl;</div><div class="line">}</div><div class="line"></div><div class="line"><span class="keywordtype">int</span> main(<span class="keywordtype">int</span> argc, <span class="keywordtype">char</span> *argv[])</div><div class="line">{</div><div class="line">    help();</div><div class="line"></div><div class="line">    <span class="keywordflow">if</span> (argc != 4)</div><div class="line">    {</div><div class="line">        cout &lt;&lt; <span class="stringliteral">&quot;Not enough parameters&quot;</span> &lt;&lt; endl;</div><div class="line">        <span class="keywordflow">return</span> -1;</div><div class="line">    }</div><div class="line"></div><div class="line">    <span class="keyword">const</span> <span class="keywordtype">string</span> source      = argv[1];           <span class="comment">// the source file name</span></div><div class="line">    <span class="keyword">const</span> <span class="keywordtype">bool</span> askOutputType = argv[3][0] ==<span class="charliteral">&#39;Y&#39;</span>;  <span class="comment">// If false it will use the inputs codec type</span></div><div class="line"></div><div class="line">    <a name="_a0"></a><a class="code" href="../../d8/dfe/classcv_1_1VideoCapture.html">VideoCapture</a> inputVideo(source);              <span class="comment">// Open input</span></div><div class="line">    <span class="keywordflow">if</span> (!inputVideo.<a name="a1"></a><a class="code" href="../../d8/dfe/classcv_1_1VideoCapture.html#a9d2ca36789e7fcfe7a7be3b328038585">isOpened</a>())</div><div class="line">    {</div><div class="line">        cout  &lt;&lt; <span class="stringliteral">&quot;Could not open the input video: &quot;</span> &lt;&lt; source &lt;&lt; endl;</div><div class="line">        <span class="keywordflow">return</span> -1;</div><div class="line">    }</div><div class="line"></div><div class="line">    string::size_type pAt = source.find_last_of(<span class="charliteral">&#39;.&#39;</span>);                  <span class="comment">// Find extension point</span></div><div class="line">    <span class="keyword">const</span> <span class="keywordtype">string</span> NAME = source.substr(0, pAt) + argv[2][0] + <span class="stringliteral">&quot;.avi&quot;</span>;   <span class="comment">// Form the new name with container</span></div><div class="line">    <span class="keywordtype">int</span> ex = <span class="keyword">static_cast&lt;</span><span class="keywordtype">int</span><span class="keyword">&gt;</span>(inputVideo.<a name="a2"></a><a class="code" href="../../d8/dfe/classcv_1_1VideoCapture.html#aa6480e6972ef4c00d74814ec841a2939">get</a>(<a name="a3"></a><a class="code" href="../../d4/d15/group__videoio__flags__base.html#ggaeb8dd9c89c10a5c63c139bf7c4f5704da53e1c28d4c2ca10732af106f3bf00613">CAP_PROP_FOURCC</a>));     <span class="comment">// Get Codec Type- Int form</span></div><div class="line"></div><div class="line">    <span class="comment">// Transform from int to char via Bitwise operators</span></div><div class="line">    <span class="keywordtype">char</span> EXT[] = {(char)(ex &amp; 0XFF) , (char)((ex &amp; 0XFF00) &gt;&gt; 8),(<span class="keywordtype">char</span>)((ex &amp; 0XFF0000) &gt;&gt; 16),(char)((ex &amp; 0XFF000000) &gt;&gt; 24), 0};</div><div class="line"></div><div class="line">    <a name="_a4"></a><a class="code" href="../../d6/d50/classcv_1_1Size__.html">Size</a> S = <a name="a5"></a><a class="code" href="../../dc/d84/group__core__basic.html#ga346f563897249351a34549137c8532a0">Size</a>((<span class="keywordtype">int</span>) inputVideo.<a class="code" href="../../d8/dfe/classcv_1_1VideoCapture.html#aa6480e6972ef4c00d74814ec841a2939">get</a>(<a name="a6"></a><a class="code" href="../../d4/d15/group__videoio__flags__base.html#ggaeb8dd9c89c10a5c63c139bf7c4f5704dab26d2ba37086662261148e9fe93eecad">CAP_PROP_FRAME_WIDTH</a>),    <span class="comment">// Acquire input size</span></div><div class="line">                  (int) inputVideo.<a class="code" href="../../d8/dfe/classcv_1_1VideoCapture.html#aa6480e6972ef4c00d74814ec841a2939">get</a>(<a name="a7"></a><a class="code" href="../../d4/d15/group__videoio__flags__base.html#ggaeb8dd9c89c10a5c63c139bf7c4f5704dad8b57083fd9bd58e0f94e68a54b42b7e">CAP_PROP_FRAME_HEIGHT</a>));</div><div class="line"></div><div class="line">    <a name="_a8"></a><a class="code" href="../../dd/d9e/classcv_1_1VideoWriter.html">VideoWriter</a> outputVideo;                                        <span class="comment">// Open the output</span></div><div class="line">    <span class="keywordflow">if</span> (askOutputType)</div><div class="line">        outputVideo.<a name="a9"></a><a class="code" href="../../dd/d9e/classcv_1_1VideoWriter.html#a0901c353cd5ea05bba455317dab81130">open</a>(NAME, ex=-1, inputVideo.<a class="code" href="../../d8/dfe/classcv_1_1VideoCapture.html#aa6480e6972ef4c00d74814ec841a2939">get</a>(<a name="a10"></a><a class="code" href="../../d4/d15/group__videoio__flags__base.html#ggaeb8dd9c89c10a5c63c139bf7c4f5704daf01bc92359d2abc9e6eeb5cbe36d9af2">CAP_PROP_FPS</a>), S, <span class="keyword">true</span>);</div><div class="line">    <span class="keywordflow">else</span></div><div class="line">        outputVideo.open(NAME, ex, inputVideo.<a class="code" href="../../d8/dfe/classcv_1_1VideoCapture.html#aa6480e6972ef4c00d74814ec841a2939">get</a>(<a class="code" href="../../d4/d15/group__videoio__flags__base.html#ggaeb8dd9c89c10a5c63c139bf7c4f5704daf01bc92359d2abc9e6eeb5cbe36d9af2">CAP_PROP_FPS</a>), S, <span class="keyword">true</span>);</div><div class="line"></div><div class="line">    <span class="keywordflow">if</span> (!outputVideo.isOpened())</div><div class="line">    {</div><div class="line">        cout  &lt;&lt; <span class="stringliteral">&quot;Could not open the output video for write: &quot;</span> &lt;&lt; source &lt;&lt; endl;</div><div class="line">        <span class="keywordflow">return</span> -1;</div><div class="line">    }</div><div class="line"></div><div class="line">    cout &lt;&lt; <span class="stringliteral">&quot;Input frame resolution: Width=&quot;</span> &lt;&lt; S.<a name="a11"></a><a class="code" href="../../d6/d50/classcv_1_1Size__.html#abfe0367b32c407ddccf5ddf92667c73d">width</a> &lt;&lt; <span class="stringliteral">&quot;  Height=&quot;</span> &lt;&lt; S.<a name="a12"></a><a class="code" href="../../d6/d50/classcv_1_1Size__.html#a1d289dce6b5d8006a54f3ee0259fc545">height</a></div><div class="line">         &lt;&lt; <span class="stringliteral">&quot; of nr#: &quot;</span> &lt;&lt; inputVideo.<a class="code" href="../../d8/dfe/classcv_1_1VideoCapture.html#aa6480e6972ef4c00d74814ec841a2939">get</a>(<a name="a13"></a><a class="code" href="../../d4/d15/group__videoio__flags__base.html#ggaeb8dd9c89c10a5c63c139bf7c4f5704dadadc646b31cfd2194794a3a80b8fa6c2">CAP_PROP_FRAME_COUNT</a>) &lt;&lt; endl;</div><div class="line">    cout &lt;&lt; <span class="stringliteral">&quot;Input codec type: &quot;</span> &lt;&lt; EXT &lt;&lt; endl;</div><div class="line"></div><div class="line">    <span class="keywordtype">int</span> channel = 2; <span class="comment">// Select the channel to save</span></div><div class="line">    <span class="keywordflow">switch</span>(argv[2][0])</div><div class="line">    {</div><div class="line">    <span class="keywordflow">case</span> <span class="charliteral">&#39;R&#39;</span> : channel = 2; <span class="keywordflow">break</span>;</div><div class="line">    <span class="keywordflow">case</span> <span class="charliteral">&#39;G&#39;</span> : channel = 1; <span class="keywordflow">break</span>;</div><div class="line">    <span class="keywordflow">case</span> <span class="charliteral">&#39;B&#39;</span> : channel = 0; <span class="keywordflow">break</span>;</div><div class="line">    }</div><div class="line">    <a name="_a14"></a><a class="code" href="../../d3/d63/classcv_1_1Mat.html">Mat</a> src, res;</div><div class="line">    vector&lt;Mat&gt; spl;</div><div class="line"></div><div class="line">    <span class="keywordflow">for</span>(;;) <span class="comment">//Show the image captured in the window and repeat</span></div><div class="line">    {</div><div class="line">        inputVideo &gt;&gt; src;              <span class="comment">// read</span></div><div class="line">        <span class="keywordflow">if</span> (src.empty()) <span class="keywordflow">break</span>;         <span class="comment">// check if at end</span></div><div class="line"></div><div class="line">        <a name="a15"></a><a class="code" href="../../d2/de8/group__core__array.html#ga0547c7fed86152d7e9d0096029c8518a">split</a>(src, spl);                <span class="comment">// process - extract only the correct channel</span></div><div class="line">        <span class="keywordflow">for</span> (<span class="keywordtype">int</span> i =0; i &lt; 3; ++i)</div><div class="line">            <span class="keywordflow">if</span> (i != channel)</div><div class="line">                spl[i] = Mat::zeros(S, spl[0].type());</div><div class="line">       <a name="a16"></a><a class="code" href="../../d2/de8/group__core__array.html#ga7d7b4d6c6ee504b30a20b1680029c7b4">merge</a>(spl, res);</div><div class="line"></div><div class="line">       <span class="comment">//outputVideo.write(res); //save or</span></div><div class="line">       outputVideo &lt;&lt; res;</div><div class="line">    }</div><div class="line"></div><div class="line">    cout &lt;&lt; <span class="stringliteral">&quot;Finished writing&quot;</span> &lt;&lt; endl;</div><div class="line">    <span class="keywordflow">return</span> 0;</div><div class="line">}</div></div><!-- fragment --> </div><!-- contents -->
<!-- HTML footer for doxygen 1.8.6-->
<!-- start footer part -->
<hr class="footer"/><address class="footer"><small>
Generated on Fri Apr 2 2021 11:36:29 for OpenCV by &#160;<a href="http://www.doxygen.org/index.html">
<img class="footer" src="../../doxygen.png" alt="doxygen"/>
</a> 1.8.13
</small></address>
<script type="text/javascript">
//<![CDATA[
addTutorialsButtons();
//]]>
</script>
</body>
</html>
